﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mortgage.DDL;
using LoanMonitor.Business;
using Mortgage.DAL;

namespace MortgageMonitor.Forms
{
    public partial class frmCreateAgreement : Form
    {
        DALclient client = new DALclient();

        public frmCreateAgreement()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void txtIDCardNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue.Equals(13))
                FindCustomer();
        }

        private void FindCustomer()
        {
            //Find in the local database
            AccountUI customer = client.getCustomerByID(txtIDCardNo.Text);
            UpdateCustomerInfo(new CustomerUI(customer));
            //Create new
            if (customer == null)
                MessageBox.Show("Chưa có thông tin về khách hàng này.", "Oops!");
        }
        private void UpdateCustomerInfo(CustomerUI customer)
        {
            if (customer != null)
            {
                txtName.Text = customer.Name;
                txtIDCardNo.Text = customer.IDcardNo;
                dtIdCardDate.Value = customer.IDcardDate;
                txtAccountNo.Text = customer.AccountNo;
                txtPhone.Text = customer.PhoneMobile;

                List<string> accounts = client.getCustomerAccounts(customer.IDcardNo);
                decimal accumlateLoan = 0;
                lstAccountNo.Items.Clear();
                foreach (string acc in accounts)
                {
                    lstAccountNo.Items.Add(acc);
                    accumlateLoan += StaticDataAccess.getAccumulateValueByAccountNo(acc);
                }
                txtAccumulateLoan.Text = accumlateLoan.ToString("N0");

                string AccountNo = (string.IsNullOrEmpty(txtAccNo.Text)) ?
                                lstAccountNo.Items[0].ToString() : txtAccNo.Text;
                txtAccNo.Text = AccountNo;
                UpdateAccountInfo(AccountNo);
                txtAccumlateValue.Text = StaticDataAccess.getAccumulateValueByAccountNo(AccountNo).ToString("N0");
            }
        }

        private void UpdateAccountInfo(string AccountNo)
        {
            AccountUI acc = new AccountUI(client.getCustomerInfo(AccountNo));
            if (acc != null)
            {
                txtAccName.Text = acc.Name;
                txtAccIdCardNo.Text = acc.IDcardNo;
                txtAccIdCardIssuer.Text = acc.IDcardIssuer;
                txtAccIdCardDate.Text = acc.IDcardDate.ToString("yyyy/MM/dd");
                txtAccIdCardIssuer.Text = acc.IDcardIssuer;
                txtAccMobile.Text = acc.PhoneMobile;
                txtAccFix.Text = acc.PhoneFix;
                txtAccContact.Text = acc.AddressContact;
                txtAccResident.Text = acc.AddressResident;
                txtAccEmail.Text = acc.Email;
                rdAccIsMale.Checked = (acc.Gender.Equals("Nam")) ? true : false;
            }
            else
            {
                MessageBox.Show("Account not found!");
            }
        }

    }
}
